package com.zqxq.lottery.api.controller;

import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author 苦智
 * @date 2018/5/25 16:05
 * @description 活动controller
 * @ClassName ActivityController
 */
@Api(value = "活动接口",tags = "活动接口")
@RestController
@Slf4j
@RequestMapping(value = "/apiActivity",produces = "application/json")
public class ActivityController {

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @ApiOperation("用户邀请活动数据统计")
    @PostMapping("/userActivity")
    public Result userActivityStatistic(@ApiParam("userId") Long userId){
        Result result = new Result();
        if (userId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        try {
            result = lotteryServerFeignClient.queryUserActivityStatistic(userId);
        }catch (Exception e){
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
            log.info("userActivity error",e);
        }
        return result;
    }

    @ApiOperation("我的-用户优惠券列表")
    @PostMapping("/getCouponList")
    public Result getCouponList(@ApiParam(value = "用户id",required = true) @RequestParam("userId") Long userId,
                                @ApiParam(value = "优惠券状态1:未使用 2:已使用、已过期 3:待派发",required = true) @RequestParam("status") Integer status,
                                @ApiParam(value = "pageNo",required = true) @RequestParam(defaultValue = "1") Integer pageNo,
                                @ApiParam(value = "pageSize",required = true) @RequestParam(defaultValue = "10") Integer pageSize){
        Result result = new Result();
        if (userId == null || status == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        try {
            result = lotteryServerFeignClient.queryUserCouponList(userId,status,pageNo,pageSize);
        }catch (Exception e){
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
            log.info("getCouponList error",e);
        }
        return result;
    }



    /**
     * @Author 苦智
     * @Description 传入待支付金额，优惠券id，返回优惠后的支付金额
     * @Date 18:11 2018/5/25
     * @Param [userId, money, orderId, payPassword]
     * @return com.zqxq.common.constant.Result
     **/
    @PostMapping("/couponConsume")
    @ApiOperation(value = "优惠券抵扣",notes = "优惠券满N减N")
    public Result couponConsume(@ApiParam(value = "优惠券id",required = true) @RequestParam("couponId") Long couponId,
                                @ApiParam(value = "订单id",required = true) @RequestParam("orderId") Long orderId){
        Result result = new Result();
        if (orderId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        try {
            result = lotteryServerFeignClient.couponConsume(couponId,orderId);
        } catch (Exception e){
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
            log.error("couponConsume error",e);
        }
        return result;
    }

    @ApiOperation("查询可用优惠券")
    @PostMapping("/getAvailableCouponList")
    public Result getCouponList(@ApiParam(value = "订单编号",required = true) @RequestParam("orderId") Long orderId){
        Result result = new Result();
        if (orderId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        try {
            result = lotteryServerFeignClient.getAvailableCouponList(orderId);
        }catch (Exception e){
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
            log.info("getAvailableCouponList error",e);
        }
        return result;
    }

    @ApiOperation("用户邀请户数排行榜")
    @PostMapping("/inviteRank")
    public Result inviteRankData(){
        Result rs = lotteryServerFeignClient.getInviteRankData();
        return rs;
    }
}
